package impl

import (
	"adam2/internal/dto"
	"adam2/internal/model"
	"time"
)

type MonitorCurrentPriceDaoImpl struct {
	*BaseDaoImpl
}

// 返回dao实现类
func GetMonitorCurrentPriceDaoImpl() *MonitorCurrentPriceDaoImpl {
	return &MonitorCurrentPriceDaoImpl{GetBaseDaoImpl()}
}

// 返回表名
func (_monitorCurrentPriceDaoImpl *MonitorCurrentPriceDaoImpl) FindTableName() string {
	return "MONITOR_CURRENT_PRICE"
}

// 添加
func (_monitorCurrentPriceDaoImpl *MonitorCurrentPriceDaoImpl) Add(monitorCurrentPrice *model.MonitorCurrentPrice) error {
	var result = _monitorCurrentPriceDaoImpl.db.Create(monitorCurrentPrice)
	return result.Error
}

// 根据条件，查询记录
func (_monitorCurrentPriceDaoImpl *MonitorCurrentPriceDaoImpl) Find(monitorCurrentPrice model.MonitorCurrentPrice) model.MonitorCurrentPriceArray {
	var monitorCurrentPriceArray model.MonitorCurrentPriceArray
	_monitorCurrentPriceDaoImpl.db.Where(&monitorCurrentPrice).Find(&monitorCurrentPriceArray)
	return monitorCurrentPriceArray
}

// 分页显示
func (_monitorCurrentPriceDaoImpl *MonitorCurrentPriceDaoImpl) Page(transactionType int, code string, name string, available int, pageNo int, pageSize int) dto.MonitorCurrentPriceDtoArray {
	var monitorCurrentPriceDtoArray dto.MonitorCurrentPriceDtoArray
	var offset int = (pageNo - 1) * pageSize
	db := _monitorCurrentPriceDaoImpl.db.Model(model.MonitorCurrentPrice{}).Select("id, name, " +
		"(CASE WHEN transaction_type = 1 THEN '股票' WHEN transaction_type = 2 THEN 'ETF' WHEN transaction_type = 3 THEN '期货' ELSE '未知' END) AS transaction_type, " +
		"code, monitor_price, " +
		"(CASE WHEN comparison = 1 THEN '大于等于' WHEN comparison = 2 THEN '小于等于' ELSE '未知' END) AS comparison, " +
		"to_char(create_time, 'yyyy-mm-dd HH24:MI:SS') AS create_time, " +
		"(CASE WHEN available = 1 THEN '可用' WHEN available = 2 THEN '不可用' ELSE '未知' END) AS available")
	db = db.Order("create_time desc").Offset(offset).Limit(pageSize)
	if transactionType != 0 && transactionType != -1 {
		db = db.Where("transaction_type = ?", transactionType)
	}
	if code != "" {
		db = db.Where("transaction_type = ?", code)
	}
	if name != "" {
		db = db.Where("name like ?", "%"+name+"%")
	}
	if available != 0 {
		db = db.Where("available = ?", available)
	}
	db.Find(&monitorCurrentPriceDtoArray)
	return monitorCurrentPriceDtoArray
}

// 分页显示，返回总数
func (_monitorCurrentPriceDaoImpl *MonitorCurrentPriceDaoImpl) PageTotal(transactionType int, code string, name string, available int) int {
	var monitorCurrentPriceDtoArray dto.MonitorCurrentPriceDtoArray
	db := _monitorCurrentPriceDaoImpl.db.Model(model.MonitorCurrentPrice{}).Select("id, name, " +
		"(CASE WHEN transaction_type = 1 THEN '股票' WHEN transaction_type = 2 THEN 'ETF' WHEN transaction_type = 3 THEN '期货' ELSE '未知' END) AS transaction_type, " +
		"code, monitor_price, " +
		"(CASE WHEN comparison = 1 THEN '大于等于' WHEN comparison = 2 THEN '小于等于' ELSE '未知' END) AS comparison, " +
		"to_char(create_time, 'yyyy-mm-dd HH24:MI:SS') AS create_time, " +
		"(CASE WHEN available = 1 THEN '可用' WHEN available = 2 THEN '不可用' ELSE '未知' END) AS available")
	db = db.Order("create_time desc")
	if transactionType != 0 && transactionType != -1 {
		db = db.Where("transaction_type = ?", transactionType)
	}
	if code != "" {
		db = db.Where("transaction_type = ?", code)
	}
	if name != "" {
		db = db.Where("name like ?", "%"+name+"%")
	}
	if available != 0 {
		db = db.Where("available = ?", available)
	}
	db.Find(&monitorCurrentPriceDtoArray)
	return len(monitorCurrentPriceDtoArray)
}

// 根据id更新available
func (_monitorCurrentPriceDaoImpl *MonitorCurrentPriceDaoImpl) UpdateAvailableById(id int, available int) {
	_monitorCurrentPriceDaoImpl.db.Model(&model.MonitorCurrentPrice{}).Where("id = ?", id).Updates(model.MonitorCurrentPrice{Available: available, UpdateTime: time.Now()})
}

// 删除监控
func (_monitorCurrentPriceDaoImpl *MonitorCurrentPriceDaoImpl) DeleteMonitorCurrentPrice(id int) {
	_monitorCurrentPriceDaoImpl.db.Where("id = ?", id).Delete(&model.MonitorCurrentPrice{})
}
